<template>
  <view class="load" v-if="!load">
      <u-loading-icon mode="circle" inactive-color="#fff" text="正在加载..." vertical></u-loading-icon>
    </view>
  <view v-else class="page">
    <block v-if="type == 1">
      <!-- 用户 -->
    <view class="group_5">
      <view class="box_2">
        <view class="text-wrapper_4">
          <text lines="1" v-for="(item, index) in tabList" :key="index" class="text_item"
            :class="{ activeNav: indexNav == item.value }" @click="onClickTab(item.value)">{{ item.label }}</text>
        </view>
      </view>

      <view class="list_2">
        <block v-if="fastContactList.length > 0">
            <view class="orderbox" v-for="(item,index) in fastContactList" :key="index" @click.stop="nav_to('/pages/teacher/re_details?id='+item.ID)">
                <view class="userbox">
                    <image class="hdimg" :src="item.TecherImg" mode="aspectFill" @click.stop="nav_to('/pages/teacher/detail?id='+item.TecherID)"></image>
                    <view class="userinfo">
                        <view class="namebox">
                            <view class="name">{{'教练：'+$util.entitiesToUtf16(item.TecherName)}}</view>
                            <view class="status" :class="classColor[item.Status]">{{onStatus(item.Status)}}</view>
                            <!-- :class="{'color1':item.Status == -1,'color2':item.Status ==1,'':item.Status ==2,'color1':item.Status ==3,'color3':item.Status == -2}" -->
                        </view>
                        <view class="starbox">
                            <view class="startitle">评价：</view>
                            <block v-for="(value, index) in starRateView(item.Score)" :key="index">
                              <image class="starimg" :src="value"></image>
                            </block>
                        </view>
                    </view>
                </view>
                <view class="adder">{{(item.Item[0].ProductName?$util.entitiesToUtf16(item.Item[0].ProductName):'未知')+' | '+(item.Place?item.Place:'未知')}}</view>
                <view class="titmebox">{{getTime2(item.ServerTime)+gettoday(item.ServerTime)+' '+ gettime(item.ServerTime,item.Hours)}}</view>
                <view class="bottombox">
                    <view class="bo_titme">
                        {{getTime(item.OrderTime)}}
                    </view>
                    <view v-if="item.Status == 0" class="bo_btn" @click.stop="Cancel(item.ID)">取消预约</view>
                    <block v-else-if="item.Status == -1 || item.Status == -2">
                      <view class="bo_btn" @click.stop="nav_to('/pages/teacher/detail?id='+item.TecherID)">重新预约</view>
                      <!-- <view v-if="item.Status == -2" class="bo_btn">查看原因</view> -->
                    </block>
                    <block v-else-if="item.Status == 3">
                      <view class="bo_btn" @click.stop="nav_to('/pages/teacher/detail?id='+item.TecherID+'&navindex=0')">查看评价</view>
                      <view class="bo_btn2" @click.stop="nav_to('/pages/teacher/user_review?id='+item.TecherID)">去评价</view>
                    </block>
                    
                </view>
            </view>
        </block>
        <block v-else>
          <view>
            <image class="kongimg" src="https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1720073678914285.png"
              mode=""></image>
            <view class="kongtext">暂无订单</view>
          </view>
        </block>
      </view>
    </view>
    </block>
    <block v-else>
      <!-- 教练 -->
      <view class="group_5">
      <view class="box_2">
        <view class="text-wrapper_4">
          <text lines="1" v-for="(item, index) in tabList" :key="index" class="text_item"
            :class="{ activeNav: indexNav == item.value }" @click="onClickTab(item.value)">{{ item.label }}</text>
        </view>
      </view>

      <view class="list_2">
        <block v-if="fastContactList.length > 0">
            <view class="orderbox" v-for="(item,index) in fastContactList" :key="index" @click.stop="nav_to('/pages/teacher/re_details?id='+item.ID+'&type=1')">
                <view class="userbox">
                    <image class="hdimg" :src="item.HeadImgUrl" mode="aspectFill"  @click.stop="nav_to('/pages/user/user_visitor?id='+item.UserID)"></image>
                    <view class="userinfo">
                        <view class="namebox">
                            <view class="name">{{$util.entitiesToUtf16(item.UserName)}}</view>
                            <view class="status" :class="classColor[item.Status]">{{onStatus(item.Status)}}</view>
                        </view>
                        <!-- <view class="starbox">
                            <view class="startitle">评价：</view>
                            <block v-for="(value, index) in starRateView(item.Score)" :key="index">
                              <image class="starimg" :src="value"></image>
                            </block>
                        </view> -->
                    </view>
                </view>
                <view class="adder">{{(item.Item[0].ProductName?$util.entitiesToUtf16(item.Item[0].ProductName):'未知')+' | '+(item.Place?item.Place:'未知')}}</view>
                <view class="titmebox">{{getTime2(item.ServerTime)+gettoday(item.ServerTime)+' '+ gettime(item.ServerTime,item.Hours)}}</view>
                <view class="bottombox">
                    <view class="bo_titme">
                        {{getTime(item.OrderTime)}}
                    </view>
                    <block v-if="item.Status == 0">
                      <view class="bo_btn" @click.stop="OrderAudit(item.ID,-2)">拒绝</view>
                      <view class="bo_btn2" @click.stop="OrderAudit(item.ID,1)">同意</view>
                    </block>
                    <!-- <view v-else-if="item.Status == 1" class="bo_btn" @click.stop="StartServer(item.ID)">开始服务</view> -->
                      <!-- <view v-else-if="item.Status == 2" class="bo_btn" @click.stop="FinishedServer(item.ID)">完成服务</view> -->
                    <block v-else-if="item.Status == 3">
                      <view class="bo_btn" @click.stop="nav_to('/pages/teacher/detail?id='+item.TecherID+'&navindex=0')">查看评价</view>
                      <view class="bo_btn2" @click.stop="isAll = true">去评价</view>
                    </block>
                    
                </view>
            </view>
        </block>
        <block v-else>
          <view>
            <image class="kongimg" src="https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1720073678914285.png"
              mode=""></image>
            <view class="kongtext">暂无订单</view>
          </view>
        </block>
      </view>
      <view class="checkbox" @click="checkEQ">核销</view>
    </view>
    </block>
    <!-- 教练评价弹窗 -->
    <view v-if="isAll" class="Shadowbox">
      <view class="Popupbox">
        <view class="title_1">发表评价</view>
        <view class="title_2">感觉和该学员合作的怎么样</view>
        <view class="starBox">
        <view class="starCell" v-for="(item, index) in evaList" :key="item.id">
            <view class="imgbox">
              <view
                class="starImg"
                v-for="(items, indexs) in 5"
                :key="indexs"
                @click="chooseStar(index, indexs)"
              >
                <image
                  class="starPic"
                  :src="whatStar(index, indexs)"
                  mode="widthFix"
                ></image>
              </view>
            </view>
          </view>
        </view>
        <view class="text">{{re_title}}</view>
        <view class="subbtn" @click="subReview">提交评价</view>
        <image class="close" src="https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/17157621475961272.png" @click.stop="isAll = false"></image>
      </view>
    </view>
    


    <uni-popup type="center" ref="checkbox">
			<view class="Successbox">
				<image class="suimg" src="https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1723017497812406.png"></image>
				<view class="sutext">核销成功！</view>
				<view class="coutbtn" @click="suopenclose">继续核销</view>
				<view class="suclose" @click="suclose">关闭</view>
			</view>
		</uni-popup>
		<uni-popup type="center" ref="Failcheckbox">
			<view class="Successbox">
				<image class="suimg" src="https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/17230184678361039.png"></image>
				<view class="sutext">核销失败</view>
				<view class="sutfiext">{{mesAge?mesAge:''}}</view>
				<view class="coutbtn" @click="Fiopenclose">继续核销</view>
				<view class="suclose" @click="Ficlose">关闭</view>
			</view>
		</uni-popup>
  </view>
</template>

<script>
export default {
  components: {
  },
  data() {
    return {
      mesAge:'',
      load:false,
      evaList: [
        {
          id: "s1",
          title: "教练评价",
          star: 0,
        },
      ],
      type:1, // 判断是否是教练还是用户
      indexNav: 99,
      fastContactList: [],
      tabList: [       //顶部状态选择
        {
          label: '全部',
          value: 99
        },
        // {
        //   label: '待同意',
        //   value: 0
        // },
        // {
        //   label: '已同意',
        //   value: 1
        // },
        {
          label:'进行中',
          value:2
        },
        // {
        //   label: '已拒绝',
        //   value: -2
        // },
        {
          label: '已完成',
          value: 3
        },
        {
            label: '已退款',
            value: -2,
        }
      ],
      classColor:{
        '0': '',
        '1': 'color2',
        '2':'color2',
        '3':'color1',
        '-1': 'color1',
        '-2': 'color3'
      },
      PageIndex: 1,
      PageSize: 10,
      isAll: false, // 教练评价弹窗
      textlist:['非常差','差','一般','满意','非常满意'],
      re_title:'',
      BarTitle:'预约管理'
    }
  },
  onLoad(e) {
    if(e.type == 1){
      this.type = e.type
      this.BarTitle = '课程订单'
      this.MyServerOrders()
    }else{
      this.type = e.type
      this.TeOrders()
    }
    uni.setNavigationBarTitle({
			title: this.BarTitle,
			success: function () {
			},
			fail: function (err) {
			}
		});
  },
  //下拉刷新
  onPullDownRefresh(){
    if(this.type == 1){
      this.PageIndex = 1
      this.fastContactList = []
      this.MyServerOrders()
    }else{
      this.PageIndex = 1
      this.fastContactList = []
      this.TeOrders()
    }
    uni.stopPullDownRefresh()
  },
  // 触底事件
  onReachBottom() {
    if(this.isMore){
      if(this.type == 1){
        this.PageIndex++
        this.MyServerOrders()
      }else{
         this.PageIndex++
         this.TeOrders()
      }
      
    }
  },
  methods: {
    Fiopenclose(){
			this.$refs['Failcheckbox'].close()
			this.checkEQ()
		},
		suopenclose(){
			this.$refs['checkbox'].close()
			this.checkEQ()
		},
		Ficlose(){
			this.$refs['Failcheckbox'].close()
		},
		suclose(){
			this.$refs['checkbox'].close()
		},
    // 点击核销
		checkEQ() {
			let that = this
			uni.scanCode({
				scanType:['qrCode'],
				success: async function (res) {
					let res1 = await that.$api.post('/ServerOrder/Verified', { 
						ActionCode: res.result,
					});
					if (res1.code == 0) {
						that.$refs['checkbox'].open()
					}else{
						that.mesAge = res1.message
						that.$refs['Failcheckbox'].open()
					}
					// that.$pv.msg(res1.message);
				}
			});
		},
    //教练提交评价
    async subReview(){
      this.isAll = !this.isAll
    },
    // 点击选择星星
    chooseStar(lindex, sindex) {
      this.evaList[lindex].star = sindex + 1;
      this.fen = sindex + 1
      this.re_title = this.textlist[sindex]
    },
    //判断是已灰色星星还是黄色（选中）状态的星星 lindex:evaList的index,sindex:选中的第几颗星星
    whatStar(lindex, sindex) {
      let staring;
      let starhalf;
      let stared;
      if (lindex == 0) {
        //总体高亮
        staring =
          "https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1719814859207878.png";
        //总体半亮
        // starhalf = 'https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/1713853365936666.png';
        //总体灰色
        stared =
          "https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/17202602010231341.png";
      } else {
        staring =
          "https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/1719814859207878.png";
        // starhalf = 'https://golfdate.oss-cn-shenzhen.aliyuncs.com/upload/17138533895031496.png';
        stared =
          "https://wavestation.oss-cn-shenzhen.aliyuncs.com/upload/17202602010231341.png";
      }

      const num = this.evaList[lindex].star;
      const starNum = sindex + 1;
      return num - starNum >= 0
        ? staring
        : num - starNum == -0.5
        ? starhalf
        : stared;
    },
    //教练完成服务
    async FinishedServer(id){
      let _this = this
      uni.showModal({
            title: "温馨提示",
            content: '您确定要完成服务吗？',
            confirmText: "确定",
            success: async function (res) {
                if (res.confirm) {
                    let res1 = await _this.$api.post('Technicians/FinishedServer',{
                        ID:id
                    })
                    if(res1.code == 0){
                        _this.PageIndex = 1
                        _this.fastContactList = []
                        _this.TeOrders()
                    }
                } else if (res.cancel) {
                }
            },
        });
    },
    //教练开始服务
    async StartServer(id){
      let _this = this
      uni.showModal({
            title: "温馨提示",
            content: '您确定要开始服务吗？',
            confirmText: "确定",
            success: async function (res) {
                if (res.confirm) {
                    let res1 = await _this.$api.post('Technicians/StartServer',{
                        ID:id
                    })
                    if(res1.code == 0){
                        _this.PageIndex = 1
                        _this.fastContactList = []
                        _this.TeOrders()
                    }
                } else if (res.cancel) {
                }
            },
        });
    },
    //教练同意和取消
    async OrderAudit(id,status){
      let _this = this
      let text = status == 1? '您确定要同意预约吗？':'您确定要拒绝预约吗？'
        uni.showModal({
            title: "温馨提示",
            content: text,
            confirmText: "确定",
            success: async function (res) {
                if (res.confirm) {
                    let res1 = await _this.$api.post('Technicians/OrderAudit',{
                        ID:id,
                        Status:status
                    })
                    if(res1.code == 0){
                        _this.PageIndex = 1
                        _this.fastContactList = []
                        _this.TeOrders()
                    }
                } else if (res.cancel) {
                }
            },
        });
    },
    //用户取消预约
    async Cancel(id){
        let _this = this
        uni.showModal({
            title: "温馨提示",
            content: "您确定要取消预约吗？",
            confirmText: "确定",
            success: async function (res) {
                if (res.confirm) {
                    let res1 = await _this.$api.post('ServerOrder/Cancel',{
                        ID:id
                    })
                    if(res1.code == 0){
                       _this.PageIndex = 1
                       _this.fastContactList = []
                        _this.MyServerOrders()
                    }
                } else if (res.cancel) {
                }
            },
        });
    },
    //教练订单
    async TeOrders(){
      // this.load = false
      let res =await this.$api.post('Technicians/Orders',{
          PageIndex:this.PageIndex,
          PageSize:this.PageSize,
          Status:this.indexNav
      })
      if(res.code == 0){
        this.fastContactList = this.fastContactList.concat(res.data)
        this.isMore =
            Array.isArray(res.data) && res.data.length >= this.PageSize;
        this.load = true
      }
    },
    //用户订单
    async MyServerOrders(){
      this.load = false
      let res = await this.$api.post('ServerOrder/MyServerOrders',{
        PageIndex:this.PageIndex,
        PageSize:this.PageSize,
        Status:this.indexNav
      })
      if(res.code == 0){
        this.fastContactList = this.fastContactList.concat(res.data)
        this.isMore =
            Array.isArray(res.data) && res.data.length >= this.PageSize;
        this.load = true
      }
    },
    onClickTab(e) {
      this.indexNav = e
      if(this.type == 1){
        this.PageIndex =1
        this.fastContactList = []
        this.MyServerOrders()
      }else{
        this.PageIndex =1
        this.fastContactList = []
        this.TeOrders()
      }
    },
    onStatus(status){
      if(status == 0){
        return '待同意'
      }else if(status == 1){
        return '已同意'
      }else if(status == 2){
        return '进行中'
      }else if(status == 3){
        return '已完成'
      }else if(status == -1){
        return '已取消'
      }else if(status == -2){
        return '已退款'
      }
    },
    gettoday(l){
      let d = new Date(l);
      let timestamp = d.getTime();
      let nowtime = new Date()
      let nowday = nowtime.getTime()
      if(this.$util.unixDatetime(nowday, 'dd') == this.$util.unixDatetime(timestamp, 'dd')){
          return '(今天)'
      }else{
        return ''
      }
    },
    getTime2(l) {
            let d = new Date(l);
            let timestamp = d.getTime();
            return this.$util.unixDatetime(timestamp, 'year');
        },
    //时长
    gettime(l,Hours){
        let d = new Date(l);
        let timestamp = d.getTime();
        let start = this.$util.unixDatetime(timestamp, 'hh-mm');
        let endtime = (Number(start.slice(0,2))+Hours)+':00'
        if((Number(start.slice(0,2))+Hours)>24){
          endtime = '次日'+((Number(start.slice(0,2))+Hours)-24)+':00'
        }
        return start+'~'+endtime
    }
  }
}
</script>

<style lang="scss">
.checkbox{
  position: fixed;
  bottom: 250rpx;
  right: 50rpx;
  width: 108rpx;
  height: 108rpx;
  background-color: #706EEE;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28rpx;
  color: #fff;
}
.page {
  background-color: rgba(246, 246, 246, 1.000000);
  display: flex;
  flex-direction: column;
  height: 100vh;

  .group_5 {
    padding-bottom: 34rpx;
    display: flex;
    flex-direction: column;
    padding-top: 54rpx;
    position: relative;
    .box_2 {
      background-color: rgba(255, 255, 255, 1.000000);
      display: flex;
      flex-direction: column;
      padding: 0 40rpx;
      padding-top: 24rpx;
      position: fixed;
      top: -5rpx;
      box-sizing: border-box;
      width: 100%;
      z-index: 10;

      .text-wrapper_4 {
        display: flex;
        justify-content: space-between;

        .text_item {
          overflow-wrap: break-word;
          color: #6f6f6f;
          font-size: 28rpx;
          font-family: PingFang-SC-Bold;
          font-weight: normal;
          text-align: right;
          white-space: nowrap;
          line-height: 40rpx;
        }

        .activeNav {
          font-weight: bold;
          color: #000000;
        }

        .activeNav::after {
          content: '';
          display: block;
          background-color: rgba(112, 110, 238, 1.000000);
          border-radius: 4rpx;
          width: 48rpx;
          height: 8rpx;
          margin: 0 auto;
          margin-top: 15rpx;
        }
      }
    }

    .list_2 {
      width: 100%;
      margin-top: 40rpx;
      .orderbox{
        width: 702rpx;
        height: fit-content;
        background: #FFFFFF;
        border-radius: 16rpx;
        margin: 0 auto;
        padding: 24rpx;
        box-sizing: border-box;
        margin-bottom: 20rpx;
        .userbox{
            display: flex;
            align-items: center;
            margin-bottom: 24rpx;
            .hdimg{
                width: 88rpx;
                height: 88rpx;
                border-radius: 50%;
                margin-right: 24rpx;
            }
            .userinfo{
                flex: 1;
                .namebox{
                    display: flex;
                    align-items: center;
                    margin-bottom: 8rpx;
                    .name{
                        font-size: 32rpx;
                        font-weight: bold;
                        color: #333333;
                        margin-right: auto;
                    }
                    .status{
                        font-size: 28rpx;
                        color: #FF7713;
                    }
                    .color1{
                        color: #999999;
                    }
                    .color2{
                        color: #2BA139;
                    }
                    .color3{
                      color: #FC201C;
                    }
                }
                .starbox{
                    display: flex;
                    align-items: center;
                    .startitle{
                        font-size: 24rpx;
                        color: #666666;
                    }
                    .starimg{
                        width: 24rpx;
                        height: 24rpx;
                    }
                }
            }
        }
        .adder{
            font-size: 28rpx;
            color: #666666;
            margin-bottom: 12rpx;
        }
        .titmebox{
            font-size: 28rpx;
            color: #666666;
            margin-bottom: 12rpx;
            height: 64rpx;
            border-bottom: 1rpx solid #E4E4E4;
        }
        .bottombox{
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 38rpx;
            .bo_titme{
                font-size: 24rpx;
                color: #999999;
                flex: 1;
            }
            .bo_btn{
                width: 160rpx;
                height: 60rpx;
                border-radius: 30rpx;
                border: 2rpx solid #E4E4E4;
                text-align: center;
                line-height: 60rpx;
                color: #333333;
                font-size: 24rpx;
                margin-left: 16rpx;
            }
            .bo_btn2{
              width: 160rpx;
                height: 60rpx;
                border-radius: 30rpx;
                // border: 2rpx solid #E4E4E4;
                background-color: #706EEE;
                text-align: center;
                line-height: 60rpx;
                color: #fff;
                font-size: 24rpx;
                margin-left: 16rpx;
            }
        }
      }
    }
  }
}
.Shadowbox{
  width: 100%;
  height: 100vh;
  background-color: rgba(0,0,0,0.5);
  position: absolute;
  .Popupbox{
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    width: 588rpx;
    height: 454rpx;
    background: #FFFFFF;
    border-radius: 24rpx;
    text-align: center;
    padding: 32rpx;
    box-sizing: border-box;
    .title_1{
      font-size: 32rpx;
      font-weight: bold;
      color: #333333;
      margin-bottom: 32rpx;
    }
    .title_2{
      font-size: 28rpx;
      color: #999999;
    }
    .text{
      color: #706EEE;
      font-size: 28rpx;
          margin-bottom: 32rpx;
    }
    .subbtn{
      width: 432rpx;
      height: 88rpx;
      background: #706EEE;
      border-radius: 44rpx;
      color: #fff;
      font-size: 32rpx;
      font-weight: bold;
      text-align: center;
      line-height: 88rpx;
      margin: 0 auto;
    }
    .close{
      width: 32rpx;
      height: 32rpx;
      position: absolute;
      right: 32rpx;
      top: 32rpx;
    }
  }
}
.starBox {
  .starCell {
    width: 100%;
    display: flex;
    align-items: center;
    margin: 10rpx 0;
    .startitle{
        font-size: 28rpx;
        color: #999999;
    }
    .starText {
      width: 140rpx;
      font-size: 32rpx;
      font-weight: bold;
      color: #333333;
    }
    .imgbox {
      flex: 1;
      display: flex;
      align-items: center;
          justify-content: center;
    }
    .starImg {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 80rpx;
      height: 70rpx;
      .starPic {
        width: 32rpx;
        height: 32rpx;
        display: block;
      }
    }
  }
  .starCell:first-child {
    .starText {
      font-size: 32rpx;
    }
    .starPic {
      width: 54rpx;
      height: 54rpx;
      display: block;
    }
  }
}
.kongimg {
  width: 330rpx;
  height: 298rpx;
  display: block;
  margin: 0 auto;
  margin-top: 102rpx;
  margin-bottom: 40rpx;
}

.kongtext {
  font-size: 28rpx;
  color: #999999;
  text-align: center;
}

.Successbox{
	width: 544rpx;
	background: #FFFFFF;
	border-radius: 48rpx;
	text-align: center;
    padding: 56rpx;
	.suimg{
		width: 152rpx;
		height: 152rpx;
	}
	.sutext{
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
		margin-top: 48rpx;
		margin-bottom: 40rpx;
	}
	.sutfiext{
		font-size: 28rpx;
		color: #999999;
		margin-bottom: 56rpx;
	}
	.coutbtn{
		width: 440rpx;
		height: 88rpx;
		background: #706EEE;
		border-radius: 44rpx;
		font-size: 28rpx;
		font-weight: bold;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.suclose{
		font-size: 28rpx;
		color: #999999;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 24rpx;
	}
}
</style>